﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Dashboard.BL.Facades;
using Dashboard.BL.Metadata;
using Dashboard.BL.ResultSet;
using GrayParrot.Core;
using GrayParrot.Core.Json;
using GrayParrot.Web.Controls;
using GrayParrot.Web.Formly;
using GrayParrot.Web.Providers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Test.DAL;

namespace Dashboard.Controllers
{
    public class EmployeeController : ApiController
    {
        [HttpGet]
        public string LoadFields()
        {
            string retValue = null;

            var m_fields = FormlyHelper.ToFormly<PalinsestoMetadata>(typeof(PalinsestoMetadata));

            //DateTime m_date = DateTime.Now.ToLocalTime();
            //DateTime m_date = new DateTime(2016, 12, 8, 20, 30, 00);
            //m_fields.DefaultValue<DateTime>("DataAvvenimento", m_date);

            retValue = FormlyHelper.Serialize(m_fields);
            //retValue = FormlyHelper.Serialize(m_fields, new List<JsonConverter> { new EventsConverter() });

            return retValue;
        }

        [HttpPost]
        public System.Net.Http.HttpResponseMessage Load(DTParameters data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.BadRequest);

            using (EmployeeFacade m_dc = EmployeeFacade.Create())
            {
                ReturnValue<IQueryable<Employee>> m_objs = m_dc.GetAll();

                DTResult<Employee> result = DTResult<Employee>.Empty("Nessun elemento trovato");

                m_objs.OnError(() =>
                {
                    result.Message.MessageType = MessageTypes.Error;
                    result.Message.Message = m_objs.Error.Message;
                }).OnSuccess(() => {
                    EmployeesResultSet m_rs = new EmployeesResultSet(data, m_objs.Value);
                    result = m_rs.GetResult();
                });



                //DTResult<Employee> result = DTResult<Employee>.Empty("Nessun elemento trovato");
                //if (m_objs.HasValue)
                //{
                //    EmployeesResultSet m_rs = new EmployeesResultSet(data, m_objs.Value);
                //    result = m_rs.GetResult();
                //}
                //else
                //{
                //    if (m_objs.HasError)
                //    {
                //        result.Message.MessageType = MessageTypes.Error;
                //        result.Message.Message = m_objs.Error.Message;
                //    }
                //}

                retValue = Request.CreateResponse(HttpStatusCode.OK, result);
            }

            return retValue;
        }
    }

    //public static class DTResultHelper
    //{
    //    public static DTResult<Employee> SetError(this DTResult<Employee> value, string message)
    //    {
    //        DTResult<Employee> retValue = value;

    //        value.Message.MessageType = MessageTypes.Error;
    //        value.Message.Message = message;

    //        return retValue;
    //    }
    //}
}